package com.shaozi.nc.aop;

import java.util.List;

import org.aspectj.lang.JoinPoint;
import org.springframework.beans.factory.annotation.Autowired;

import com.shaozi.data.mq.business.base.SourceTypeConsts;
import com.shaozi.data.mq.business.bean.MqTargetUserBean;
import com.shaozi.nc.data.model.NoticeRecordNumModel;
import com.shaozi.nc.data.model.SecretaryModel;
import com.shaozi.nc.manage.NoticeRecordNumManage;

public abstract class BaseManagePushTypeAop {
	@Autowired
	private NoticeRecordNumManage noticeRecordNumManage;

	public void baseAfterExecute(JoinPoint jp, Object retVal) {
		if (!(retVal instanceof List)) {
			return;
		}
		List<SecretaryModel> pushes = (List<SecretaryModel>) retVal;
		if (pushes == null || pushes.size() <= 0) {
			return;
		}
		for (SecretaryModel model : pushes) {
			if (model == null)
				continue;
			Integer sourceType = model.getSourceType();
			if (sourceType == null)
				continue;

			Integer type = getType(sourceType);
			if (type == null)
				continue;
			List<MqTargetUserBean> users = model.getUsers();
			if (users == null || users.size() <= 0) {
				continue;
			} else {
				int size = users.size();
				noticeRecordNumManage.addInc(model.getCompanyId(), type, size);
				noticeRecordNumManage.addIncByNotCompany(type, size);
			}

		}
	}

	private boolean between(int val, int low, int high) {
		return (val >= low) && val < high;
	}

	private Integer getType(Integer sourceType) {
		if (SourceTypeConsts.Other.Badge == sourceType) {
			return NoticeRecordNumModel.type_badge;
		} else {
			if (between(sourceType, SourceTypeConsts.Im.Base, SourceTypeConsts.Push_Max)) {
				return NoticeRecordNumModel.type_notice;
			} else if (between(sourceType, SourceTypeConsts.Data.BriefReport.Base, SourceTypeConsts.Data.BriefReport.Base_max)) {
				return NoticeRecordNumModel.type_briefReport;
			} else {
				return null;
			}
		}
	}
}
